//
//  ForeignViewController.m
//  GoodBuy
//
//  Created by dlios on 15-5-14.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "ForeignViewController.h"
#import "ForeignCommentViewController.h"
#import "DWBubbleMenuButton.h"
#import "ForeignCollectHandle.h"

#import "YBBuyWebViewController.h"


@interface ForeignViewController ()

@property(nonatomic,retain)UIImageView *picture;
@property(nonatomic,retain)UILabel *title1;
@property(nonatomic,retain)UILabel *price;
@property(nonatomic,retain)UILabel *mall;
@property(nonatomic,retain)UILabel *date;
@property(nonatomic,retain)UILabel *comment;
@property(nonatomic,retain)UILabel *collection;
@property(nonatomic,retain)UILabel *filter_content;

@property(nonatomic,assign)CGFloat F;
@property(nonatomic,retain)NSMutableArray *ForeignView;
@property (nonatomic, retain)UIScrollView *scroll1;
@property (nonatomic, assign)int flag;
@end

@implementation ForeignViewController
- (void)dealloc
{
    [_title1 release];
    [_ForeignView release];
    [_scroll1 release];
    [_article_url release];
    [_article_title release];
    [_article_price release];
    [_article_pic release];
    [_article_mall release];
    [_article_link release];
    [_article_id release];
    [_article_date release];
    [_article_comment release];
    [_article_collection release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title =_str;
   self.navigationController.navigationBar.translucent =YES ;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shoucang"] style:UIBarButtonItemStyleDone target:self action:@selector(shoucang)];
    
     self.hidesBottomBarWhenPushed=YES;
    
    [self getDataFromController];
    self.flag = 0;
}

-(void)getchuangjianshitu
{
    
    // 创建滑动视图
    self.scroll1 = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    _scroll1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scroll1];
    [_scroll1 release];
    //创建视图
    self.picture = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/6, [UIScreen mainScreen].bounds.size.height/16, [UIScreen mainScreen].bounds.size.width/1.5, [UIScreen mainScreen].bounds.size.height/3)];
    _picture.contentMode = 1;
    [_picture sd_setImageWithURL:[NSURL URLWithString: _article_pic]placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    [_scroll1 addSubview:_picture];
    [_picture release];
    
    
    //    CGFloat F = [[self class] heightWithText:_title1.text];
    
    
    
    
    
    
    self.title1 = [[UILabel alloc] initWithFrame:CGRectMake(_picture.frame.size.width/16, _picture.frame.size.height + _picture.frame.size.height*0.27, _picture.frame.size.width + _picture.frame.size.width/3, _picture.frame.size.height/2)];
    
    _title1.text = _article_title;
    _title1.backgroundColor = [UIColor whiteColor];
    _title1.numberOfLines = 0;
//    [_title1 sizeToFit];
    //设置字体
    _title1.font = [UIFont fontWithName:@"Arial" size:20];
    [_scroll1 addSubview:_title1];
    [_title1 release];
    
    self.UIVIEW = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-49, [UIScreen mainScreen].bounds.size.width, 49)];
    _UIVIEW.alpha = 0.9;
    _UIVIEW.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_UIVIEW];
    
    self.UIVIEWONE = [[UIView alloc] initWithFrame:CGRectMake(0, _UIVIEW.frame.origin.y - 140,[UIScreen mainScreen].bounds.size.width/5,190)];
    _UIVIEWONE.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_UIVIEWONE];
    
    //购买
    UIButton *QQ = [UIButton buttonWithType:UIButtonTypeSystem];
    QQ.frame = CGRectMake(_UIVIEW.frame.size.width/2.2, 0, 40, 40);
    [QQ setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [QQ addTarget:self action:@selector(BUYBUY) forControlEvents:UIControlEventTouchUpInside];
    [_UIVIEW addSubview:QQ];
    
    //分享
    UIButton *YY = [UIButton buttonWithType:UIButtonTypeSystem];
    YY.frame = CGRectMake(_UIVIEW.frame.size.width/5, 0, 40, 40);
    [YY setImage:[UIImage imageNamed:@"iconfont-houtui"] forState:UIControlStateNormal];
    [YY addTarget:self action:@selector(zhichi) forControlEvents:UIControlEventTouchUpInside];
    [_UIVIEW addSubview:YY];
    
    //评论
    UIButton *MM = [UIButton buttonWithType:UIButtonTypeSystem];
    MM.frame = CGRectMake(_UIVIEW.frame.size.width*0.7, 5, 40, 40);
    [MM setImage:[UIImage imageNamed:@"iconfont-pinglun-2"] forState:UIControlStateNormal];
    [MM addTarget:self action:@selector(pinglun) forControlEvents:UIControlEventTouchUpInside];
    [_UIVIEW addSubview:MM];
    
    
    CGFloat F = [[self class] heightWithText:_article_price];
    
    self.price = [[UILabel alloc] initWithFrame:CGRectMake(_picture.frame.size.width/16, _title1.frame.origin.y + _title1.frame.size.height, _title1.frame.size.width,F + 30)];
//    NSLog(@"4444%f",F);
    _price.text = _article_price;
//    NSLog(@"666%@",_price.text);
//                _title1.frame.size.height)
    [_price setTextColor:[UIColor redColor]];
//    _price.backgroundColor = [UIColor whiteColor];
    _price.numberOfLines = 0;
//    [_price sizeToFit];
    [_price setFont:[UIFont systemFontOfSize:20]];
    [_scroll1 addSubview:_price];
    [_price release];
    

    
    self.mall = [[UILabel alloc] initWithFrame:CGRectMake(_picture.frame.size.width/12, _picture.frame.size.height + _picture.frame.size.height/4.5, _picture.frame.size.width/1.5, _title1.frame.size.height/4)];
    _mall.text = _article_mall;
//    _mall.backgroundColor = [UIColor whiteColor];
    _mall.numberOfLines = 0;
    [_mall setFont:[UIFont systemFontOfSize:15]];
    [_mall setShadowColor:[UIColor yellowColor]];
    [_scroll1 addSubview:_mall];
    [_mall release];
    
    
    
    self.date = [[UILabel alloc] initWithFrame:CGRectMake(_picture.frame.size.width/1.5, _mall.frame.origin.y, _picture.frame.size.width/1.2, _title1.frame.size.height/4)];
    _date.text = _article_date;
//    _date.backgroundColor = [UIColor whiteColor];
    _date.numberOfLines = 0;
    [_date setFont:[UIFont systemFontOfSize:15]];
    [_date setShadowColor:[UIColor yellowColor]];
    [_scroll1 addSubview:_date];
    [_date release];
    

    
    
}

#warning mark - 分享
- (void)zhichi
{

    NSString * str = [NSString stringWithFormat:@"#GoodBuy推荐#我正在看%@,这是个非常经典的商品,都来看看吧!%@", self.ForStu.article_title,self.ForStu.article_link];
    ;
    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.listModel.article_pic]];
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5551988067e58eb48d00103b"
                                      shareText:str
                                     shareImage:imageView.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,UMShareToFacebook,UMShareToTwitter,UMShareToSms,nil]
                                       delegate:self];
}
#warning mark - 购买
- (void)BUYBUY
{
    YBBuyWebViewController *buyWebVC = [[YBBuyWebViewController alloc]init];
    buyWebVC.webUrl = _ForStu.article_link;
    buyWebVC.titleStr = _ForStu.article_title;
    [self presentViewController:buyWebVC animated:YES completion:^{
        
    }];
}

#warning mark - 评论
- (void)pinglun
{
    ForeignCommentViewController *TT = [[ForeignCommentViewController alloc] init];
    
    TT.lastModel = _ForStu;
//    TT.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:TT animated:YES];
    
//    TT.hidesBottomBarWhenPushed = NO;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}



#warning mark -收藏;
- (void)shoucang
{
   // ForeignCollectHandle *temp = [ForeignCollectHandle shareInstance];
   //[temp openMc];
//    [temp createMC];
    [[ForeignCollectHandle shareInstance]openMc];
    [[ForeignCollectHandle shareInstance]createMC];
    
    NSArray *array = [[ForeignCollectHandle shareInstance] selectAllForeign];
    
    for (ForeignCOntroller *ForeignDetail in array) {
//        NSLog(@"%@",_ForStu.article_id);
//        NSLog(@"%@",ForeignDetail.article_id);
        if ([self.ForStu.article_id isEqualToString:ForeignDetail.article_id]) {
            
           self.flag = 1;
            
            NSLog(@"flag = %d", self.flag);
            break;
        }
    }
//    NSLog(@"%d",self.flag);
    if (self.flag) {
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"已经收藏过了" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//        NSLog(@"544");
        [alert show];
    }else{
        [[ForeignCollectHandle shareInstance]insertForeign:self.ForStu];
        UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"收藏成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [aler show];
    }
    
}


#warning  mark - lable自适应高度
+ (CGFloat)heightWithText:(NSString *)text
{
    //    设置一个字典接受
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
    //    预设一个尺寸
    CGSize size = CGSizeMake(375, 1000);
    
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //    返回高度
    return rect.size.height;
}

#warning mark - 收据解析
- (void)getDataFromController
{
    NSString *idUrl = [NSString stringWithFormat:@"http://api.smzdm.com/v1/haitao/articles/%@?f=iphone",self.listModel.article_id];
    
    [NetworkHandler requestWitURL:idUrl  completion:^(id result) {
        
//                NSLog(@"%@",idUrl);
        self.ForeignView = [NSMutableArray array];
        NSDictionary *dic = [result objectForKey:@"data"];
        //                NSLog(@"%@",dic);
        
        _ForStu = [[ForeignCOntroller alloc] initWithDic:dic];
        
        [_ForeignView addObject:_ForStu];
        //        NSLog(@"%@",_ForStu.article_filter_content);
        [_ForStu release];
        self.WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, _price.frame.origin.y + _price.frame.size.height,375, 1000)];
        _WebView.delegate = self;
        [_WebView loadHTMLString:_ForStu.article_filter_content baseURL:nil];
        [_scroll1 addSubview: _WebView];
        //        NSLog(@"111%@",_ForStu.article_filter_content);
        [_WebView release];
        
        [self.HUD removeFromSuperview];
        
        
    }];
    [self getchuangjianshitu];
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_HUD];
    _HUD.labelText = @"亲 请稍等";
    _HUD.tintColor = [UIColor cyanColor];
    [_HUD showAnimated:YES whileExecutingBlock:^{
        //刷新时间最大上限
        [self showtime];
    } completionBlock:^{
        
    }];
    
    
    
}
- (void)showtime
{
    sleep(10);
}


//wabView 自适应高度
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
    int height = [height_str intValue];
//    NSLog(@"999999%@", height_str);
    _WebView.frame = CGRectMake(0,_price.frame.origin.y + _price.frame.size.height,[UIScreen mainScreen].bounds.size.width,height);
    
    _scroll1.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,height+_price.frame.origin.y +100);
}


- (void)test:(UIButton *)sender {
    
    if ([sender viewWithTag:1000]) {
        ForeignCommentViewController *TT = [[ForeignCommentViewController alloc] init];
        
        TT.lastModel = _ForStu;
        TT.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:TT animated:YES];
        
        TT.hidesBottomBarWhenPushed = NO;
    }else if ([sender viewWithTag:1001]){
        YBBuyWebViewController *buyWebVC = [[YBBuyWebViewController alloc]init];
        buyWebVC.webUrl = _ForStu.article_link;
        buyWebVC.titleStr = _ForStu.article_title;
        [self presentViewController:buyWebVC animated:YES completion:^{
            
        }];
        
    }
    
}

//状态栏隐藏
//- (BOOL)prefersStatusBarHidden {
//    return true;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
